//
//  BJOpenCaiNetHttpManager.m
//  小码哥彩票
//
//  Created by Rip on 17/4/24.
//  Copyright © 2017年 simplyou. All rights reserved.
//

#import "BJOpenCaiNetHttpManager.h"
static BJOpenCaiNetHttpManager* manager;
@implementation BJOpenCaiNetHttpManager
+(BJOpenCaiNetHttpManager *)defualtManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       
        manager = [[self alloc]init];
        
    });
    
    return manager;
}
-(void)getOpenCaiNetDataWithType:(OpenCaiNetType)type compelet:(compeletBlock)bolck
{
    NSString* typeStr = nil;
    switch (type) {
        case OpenCaiNetType_SSQ:
        {
            typeStr = @"ssq";
        }
            break;
        case OpenCaiNetType_DLT:
        {
            typeStr = @"dlt";
        }
            break;
        case OpenCaiNetType_FC3D:
        {
            typeStr = @"fc3d";
        }
            break;
        case OpenCaiNetType_PL3:
        {
            typeStr = @"pl3";
        }
            break;
        case OpenCaiNetType_PL5:
        {
            typeStr = @"pl5";
        }
            break;
        case OpenCaiNetType_QLC:
        {
            typeStr = @"qlc";
        }
            break;
        case OpenCaiNetType_QXC:
        {
            typeStr = @"qxc";
        }
            break;
        case OpenCaiNetType_TJSSC:
        {
            typeStr = @"tjssc";
        }
        default:
            break;
    }
    NSString* count = @"20";
    NSString* getUrl = [NSString stringWithFormat:@"http://f.apiplus.cn/%@-%@.json",typeStr,count];
    
    NSURLSession* session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:getUrl]];
    
    NSURLSessionTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (data==nil) {
            bolck(NO,nil);
            return;
        }
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        if (json==nil) {
            bolck(NO,nil);
            return;
        }
        id dataArray = [json objectForKey:@"data"];
        if ([dataArray isKindOfClass:[NSArray class]]) {
             bolck(YES,dataArray);
        }else{
             bolck(NO,nil);
        }
    
//        NSLog(@"%@",json);
        
    }];
    [task resume];
}
@end
